package com.neu.view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.neu.pojo.Model;
import com.neu.pojo.Worker;
import com.neu.service.impl.ModelService;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

public class AddModelUI extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private ModelService modelService = ModelService.getInstance();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddModelUI frame = new AddModelUI(new Worker());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddModelUI(Worker w) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 456, 391);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("模板 ID");
		lblNewLabel.setBounds(110, 67, 58, 15);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("模板名字");
		lblNewLabel_1.setBounds(110, 123, 58, 15);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("模板类型");
		lblNewLabel_2.setBounds(110, 187, 58, 15);
		contentPane.add(lblNewLabel_2);
		
		textField = new JTextField();
		textField.setBounds(224, 64, 66, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(224, 120, 66, 21);
		contentPane.add(textField_1);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(224, 184, 66, 21);
		contentPane.add(textField_2);
		
		JButton btnNewButton = new JButton("添加");
		btnNewButton.setBounds(97, 270, 97, 23);
		contentPane.add(btnNewButton);
		btnNewButton.addActionListener(new ActionListener() {//添加按钮的监听
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(textField.getText().length() != 0&&textField_1.getText().length() != 0&&textField_2.getText().length() != 0) {
					if(modelService.add(new Model(Integer.parseInt(textField.getText()),textField_1.getText(),textField_2.getText()))) {
						//如果用户全部都输入了
						JOptionPane.showMessageDialog(null, "添加成功");//提示成功
						ModelUI modelUI = new ModelUI(w);//跳转到新界面
						modelUI.setVisible(true);
						setVisible(false);
					}else {
						JOptionPane.showMessageDialog(null, "添加失败");//提示添加失败
					}
				}else {
					JOptionPane.showMessageDialog(null, "请输入内容");//提示用户输入完整
				}
				
			}
		});
		
		JButton btnNewButton_1 = new JButton("取消");
		btnNewButton_1.setBounds(226, 270, 97, 23);
		contentPane.add(btnNewButton_1);
		btnNewButton_1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {//跳转到上一级窗口
				ModelUI modelUI = new ModelUI(w);
				modelUI.setVisible(true);
				setVisible(false);
			}
		});
		
		//窗口居中
		setLocationRelativeTo(null);
	}
}
